package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.TBaseProjectDetails;
import com.ruoyi.system.domain.TSpCountTax;
import com.ruoyi.system.mapper.TBaseProjectDetailsMapper;
import com.ruoyi.system.service.ITSpCountTaxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TBaseProjectMapper;
import com.ruoyi.system.domain.TBaseProject;
import com.ruoyi.system.service.ITBaseProjectService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-21
 */
@Service
public class TBaseProjectServiceImpl implements ITBaseProjectService 
{
    @Autowired
    private TBaseProjectMapper tBaseProjectMapper;

    @Autowired
    private TBaseProjectDetailsMapper projectDetailsMapper;

    @Autowired
    private ITSpCountTaxService taxService;

    @Autowired
    private TSpProjectDetailServiceImpl projectDetailService;

    /**
     * 查询项目
     * 
     * @param projectId 项目主键
     * @return 项目
     */
    @Override
    public TBaseProject selectTBaseProjectByProjectId(Long projectId)
    {
        TBaseProject tBaseProject = tBaseProjectMapper.selectTBaseProjectByProjectId(projectId);
        TBaseProjectDetails details = new TBaseProjectDetails();
        details.setProjectId(projectId);
        List<TBaseProjectDetails> tBaseProjectDetails = projectDetailsMapper.selectTBaseProjectDetailsList(details);
        tBaseProject.setDetailsList(tBaseProjectDetails);
        return tBaseProject;
    }

    /**
     * 查询项目列表
     * 
     * @param tBaseProject 项目
     * @return 项目
     */
    @Override
    public List<TBaseProject> selectTBaseProjectList(TBaseProject tBaseProject)
    {
        return tBaseProjectMapper.selectTBaseProjectList(tBaseProject);
    }

    /**
     * 新增项目
     * 
     * @param tBaseProject 项目
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertTBaseProject(TBaseProject tBaseProject)
    {
        int i = tBaseProjectMapper.insertTBaseProject(tBaseProject);
        List<TBaseProjectDetails> detailsList = tBaseProject.getDetailsList();
        if(detailsList != null && detailsList.size() > 0){

            detailsList.forEach(map -> map.setProjectId(tBaseProject.getProjectId()));
            projectDetailsMapper.batchInsertTBaseProjectDetails(detailsList);
        }
        //处理月统计数据
        detailsList.stream()
//                .filter(d -> d.getBillMoney().compareTo(BigDecimal.ZERO) != 0 && d.getTaxRate().compareTo(BigDecimal.ZERO) != 0)
                .forEach(detail -> {
                    //收入合计
                    BigDecimal incomeMoney = BigDecimal.ZERO;
                    //支出合计
                    BigDecimal payMoney = BigDecimal.ZERO;
                    //项目周期明细表 取税率 和开票金额
                    TBaseProjectDetails one = detail;
                    //税率
                    BigDecimal taxRate = BigDecimal.ZERO;
                    //开票金额
                    BigDecimal billMoney = BigDecimal.ZERO;
                    //税额
                    BigDecimal taxMoney = BigDecimal.ZERO;
                    if(ObjectUtil.isNotNull(one)){
                        taxRate = one.getTaxRate();
                        billMoney = one.getBillMoney();
                        if(BigDecimal.ZERO.compareTo(taxRate) != 0 && BigDecimal.ZERO.compareTo(billMoney) != 0){
//                            incomeMoney = incomeMoney.add(billMoney); //开票金额不算到收入合计里
                            taxMoney = taxRate.multiply(billMoney).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
                            payMoney = payMoney.add(taxMoney);
                        }
                    }
            //利润合计
            BigDecimal profitMoney = incomeMoney.subtract(payMoney);
            //利润÷收入X100=利润率
            BigDecimal profitRate = (BigDecimal.ZERO.compareTo(incomeMoney) == 0 ? BigDecimal.ZERO : profitMoney.divide(incomeMoney, 3, BigDecimal.ROUND_HALF_UP)).multiply(new BigDecimal(100));
            TSpCountTax tax = new TSpCountTax();
            tax.setProjectId(detail.getProjectId());
            tax.setCountDate(one.getCountDate());
            tax.setTaxRate(taxRate);
            tax.setBillMoney(billMoney);
            tax.setTaxMoney(taxMoney);
            tax.setIncomeMoney(incomeMoney);
            tax.setPayMoney(payMoney);
            tax.setProfitMoney(profitMoney);
            tax.setProfitRate(profitRate);
            tax.setCreateDate(DateUtils.getNowDate());
            tax.setCreatePerson("SYSTEM");
            if(incomeMoney.compareTo(BigDecimal.ZERO) != 0 || payMoney.compareTo(BigDecimal.ZERO) != 0 ){
                taxService.save(tax);
            }
        });
        return i;
    }

    /**
     * 修改项目
     * 
     * @param tBaseProject 项目
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateTBaseProject(TBaseProject tBaseProject)
    {
        List<TBaseProjectDetails> detailsList = tBaseProject.getDetailsList();
        if(detailsList != null && detailsList.size() > 0){
            detailsList.forEach(map -> map.setProjectId(tBaseProject.getProjectId()));
            projectDetailsMapper.deleteTBaseProjectDetailsByPdId(tBaseProject.getProjectId());
            projectDetailsMapper.batchInsertTBaseProjectDetails(detailsList);
            //清理全部月统计数据
            taxService.remove(new LambdaQueryWrapper<TSpCountTax>()
                    .eq(TSpCountTax :: getProjectId,tBaseProject.getProjectId()));
            //重新月统计数据
            detailsList.stream()
//                    .filter(d -> d.getBillMoney().compareTo(BigDecimal.ZERO) != 0 && d.getTaxRate().compareTo(BigDecimal.ZERO) != 0)
                    .forEach(detail ->{
                        projectDetailService.processingTaxData(detail.getProjectId(),detail.getCountDate());
                    });
        }
        return tBaseProjectMapper.updateTBaseProject(tBaseProject);
    }

    /**
     * 批量删除项目
     * 
     * @param projectIds 需要删除的项目主键
     * @return 结果
     */
    @Override
    public int deleteTBaseProjectByProjectIds(Long[] projectIds)
    {
        return tBaseProjectMapper.deleteTBaseProjectByProjectIds(projectIds);
    }

    /**
     * 删除项目信息
     * 
     * @param projectId 项目主键
     * @return 结果
     */
    @Override
    public int deleteTBaseProjectByProjectId(Long projectId)
    {
        return tBaseProjectMapper.deleteTBaseProjectByProjectId(projectId);
    }
}
